﻿using System;
using System.Text;
using CodeLibrary.NativeMethods.Native;

namespace CodeLibrary.NativeMethods.Utils
{
    public static class User32Utils
    {
        public static int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong)
        {
            return Native.NativeMethods.SetWindowLong(hWnd, nIndex, dwNewLong);
        }

        public static IntPtr GetParent(IntPtr hWnd)
        {
            return Native.NativeMethods.GetParent(hWnd);
        }

        public static bool EnumChildWindows(
            IntPtr parentHandle, Win32Callback callback, IntPtr lParam)
        {
            return Native.NativeMethods.EnumChildWindows(
                parentHandle, callback, lParam);
        }

        public static IntPtr GetDesktopWindow()
        {
            return Native.NativeMethods.GetDesktopWindow();
        }

        public static IntPtr FindWindowEx(
            IntPtr hwndParent, IntPtr hwndChildAfter, 
            string lpszClass, string lpszWindow)
        {
            return Native.NativeMethods.FindWindowEx(
                hwndParent, hwndChildAfter, lpszClass, lpszWindow);
        }

        public static IntPtr FindWindow(string lpszClass, string lpszWindow)
        {
            return Native.NativeMethods.FindWindow(lpszClass, lpszWindow);
        }

        public static int GetWindowText(
            IntPtr hWnd, StringBuilder lpString, int nMaxCount)
        {
            return Native.NativeMethods.GetWindowText(hWnd, lpString, nMaxCount);
        }

        public static int GetWindowTextLength(IntPtr hWnd)
        {
            return Native.NativeMethods.GetWindowTextLength(hWnd);
        }

        public static IntPtr SendMessage(
            IntPtr hWnd, UInt32 msg, IntPtr wParam, StringBuilder lParam)
        {
            return Native.NativeMethods.SendMessage(hWnd, msg, wParam, lParam);
        }

        public static IntPtr SendMessage(
            IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)
        {
            return Native.NativeMethods.SendMessage(hWnd, msg, wParam, lParam);
        }

        public static string GetWindowTextRaw(IntPtr hwnd)
        {
            var sb = new StringBuilder();
            var length = (int) SendMessage(
                hwnd, NativeConsts.WM_GETTEXTLENGTH, IntPtr.Zero, sb);
            sb = new StringBuilder(length + 1);
            SendMessage(
                hwnd, NativeConsts.WM_GETTEXT, (IntPtr) sb.Capacity, sb);
            return sb.ToString();
        }

        public static bool SetWindowPos(
            IntPtr hWnd, IntPtr hWndInsertAfter, 
            int x, int y, int cx, int cy, SetWindowPosFlags uFlags)
        {
            return Native.NativeMethods.SetWindowPos(
                hWnd, hWndInsertAfter, x, y, cx, cy, uFlags);
        }

        public static bool GetWindowRect(IntPtr hwnd, out RECT lpRect)
        {
            return Native.NativeMethods.GetWindowRect(hwnd, out lpRect);
        }

        public static long SetParent(IntPtr hWndChild, IntPtr hWndNewParent)
        {
            return Native.NativeMethods.SetParent(hWndChild, hWndNewParent);
        }

        public static bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint)
        {
            return Native.NativeMethods.MoveWindow(hwnd, x, y, cx, cy, repaint);
        }

        public static bool PostMessage(IntPtr hwnd, uint Msg, long wParam, long lParam)
        {
            return Native.NativeMethods.PostMessage(hwnd, Msg, wParam, lParam);
        }
    }
}